package com.syxp.pda.action.plan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.base.DepartmentService;
import com.syxp.pda.service.plan.DutyUserService;

public class DutyUserAction extends BaseAction {

	@Resource
	private DutyUserService dutyuserService;
	@Resource
	private DepartmentService departmentService;
	
	private int departmentID;
	private int userID;
	private int dutyID;
	
	public String getDutyUsers() {
		try {
			List<User> dutyusers = this.dutyuserService.getDutyUsers(departmentID);
			List<Map<String, Comparable>> mlist = new ArrayList<Map<String, Comparable>>();
			for(User user: (List<User>)dutyusers) {
				Map<String, Comparable> map = new HashMap<String, Comparable>();
				map.put("operatorCode", user.getOperatorCode());
				map.put("userName", user.getUserName());
				map.put("dutyName", ((Duty)user.getDutys().toArray()[0]).getDutyName());
				map.put("userID", user.getUserID());
				map.put("dutyID", ((Duty)user.getDutys().toArray()[0]).getDutyID());
				mlist.add(map);
			}
			this.jsonString = gson.toJson(mlist);
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String delete() {
		System.out.println(this.dutyID+"==="+this.userID);
		try {
			this.dutyuserService.remove(userID, dutyID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	
	
	public String getUsers() {System.out.println(this.departmentID);
		List users = new ArrayList();
		try {
			Set<User> set = this.departmentService.getUsers(departmentID);
			for(User u: set) {
				Map map = new HashMap();
				map.put("userID", u.getUserID());
				map.put("userName", u.getUserName());
				map.put("userOptName", u.getOperatorCode()+"-"+u.getUserName());
				users.add(map);
				map = null;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = gson.toJson(users);
		return SUCCESS;
	}
	
	public String getDutys() {
		List dutys = new ArrayList();
		try {
			Set<Duty> set = this.departmentService.getDutys(departmentID);
			for(Duty d: set) {
				Map map = new HashMap();
				map.put("dutyID", d.getDutyID());
				map.put("dutyName", d.getDutyName());
				dutys.add(map);
				map = null;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = gson.toJson(dutys);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String add() {
		try {
			this.dutyuserService.add(userID, dutyID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String modify() {
		try {
			this.dutyuserService.modify(userID, dutyID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}

	/**
	 * @return the dutyuserService
	 */
	public DutyUserService getDutyuserService() {
		return dutyuserService;
	}

	/**
	 * @param dutyuserService the dutyuserService to set
	 */
	public void setDutyuserService(DutyUserService dutyuserService) {
		this.dutyuserService = dutyuserService;
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the departmentID
	 */
	public int getDepartmentID() {
		return departmentID;
	}

	/**
	 * @param departmentID the departmentID to set
	 */
	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	/**
	 * @return the userID
	 */
	public int getUserID() {
		return userID;
	}

	/**
	 * @param userID the userID to set
	 */
	public void setUserID(int userID) {
		this.userID = userID;
	}

	/**
	 * @return the dutyID
	 */
	public int getDutyID() {
		return dutyID;
	}

	/**
	 * @param dutyID the dutyID to set
	 */
	public void setDutyID(int dutyID) {
		this.dutyID = dutyID;
	}
	
}
